<template>
  <div id="detail">
    <!-- 顶部导航 -->
    <detail-nav-bar class='detail-nav' @navClick='navClick' ref='nav' />
    <!-- 可滚动内容区 -->
    <scroll 
      class='content'
      ref='scroll'
      :probe-type='3'
      :pull-up-load='false'
      @scroll="contentScroll"
    >
      <!-- 顶部轮播图 -->
      <detail-swiper :top-images="topImages"/>
      <!-- 商品信息展示 -->
      <detail-base-info 
        :goods='goods'
      />
      <!-- 店铺信息展示 -->
      <detail-shop-info :shop='shop' />
      <!-- 商品详情信息展示 -->
      <detail-goods-info 
        :detail-info='detailInfo'
        @imageLoad='imageLoad'
      />
      <!-- 商品参数展示 -->
      <detail-param-info  
        :param-info="paramInfo"
        ref='params'
      /> 
      <!-- 用户评论展示 -->
      <detail-comment-info 
        :commentInfo="commentInfo"
        ref='comment'
      />
      <!-- 推荐信息展示 -->
      <goods-list 
        :goods='recomments'
        ref='recomnd'
      />
    </scroll>
    <!-- 返回顶部组件 .nativ 监听组件的原生事件时使用-->
    <back-top @click.native="backTop" v-show="isShowBackTop"/>
    <!-- 底部工具栏 -->
    <detail-bottom-bar @addCart='addToCart'/>

    <!-- <toast :message='message' :show='show'></toast> -->
  </div>
</template>

<script>
import DetailNavBar from './childComps/DetailNavBar.vue'
import DetailSwiper from './childComps/DetailSwiper.vue'
import DetailBaseInfo from './childComps/DetailBaseInfo.vue'
import DetailShopInfo from './childComps/DetailShopInfo.vue'
import DetailGoodsInfo from './childComps/DetailGoodsInfo.vue'
import DetailParamInfo from './childComps/DetailParamInfo.vue'
import DetailCommentInfo from './childComps/DetailCommentInfo.vue'
import DetailBottomBar from './childComps/DetailBottomBar.vue'


import Scroll from 'components/common/scroll/Scroll.vue'
import GoodsList from 'components/content/goods/GoodsList.vue'

// import Toast from 'components/common/toast/Toast'

// 导入混入 
import {itenListenerMixin, BackTopMixin} from 'common/mixin.js'

import {getDetail, Goods, Shop, GoodsParam, getRecommentd} from 'network/detail'

// 导入 vuex 的 mapActions 映射 
import {mapActions} from 'vuex'

export default {
  name: 'Detail',
  components:{
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar,
    Scroll,
    GoodsList,
    // Toast
  },
  mixins: [itenListenerMixin, BackTopMixin],
  data(){
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recomments: [],
      themeTopYs: [],
      currentIndex: 0,
      // message: '',
      // show: false
    }
  },
  created(){
    // 1. 获取商品的iid 
    
    this.iid = this.$route.params.iid

    // 2. 根据商品iid请求数据
    getDetail(this.iid)
      .then(res => {
        let data = res.result

        // 1.获取顶部轮播图数据
        this.topImages = data.itemInfo.topImages

        // 2. 获取 Goods 信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

        // 3.创建店铺信息
        this.shop = new Shop(data.shopInfo)

        // 4. 保存商品的详情信息
        this.detailInfo = data.detailInfo

        // 5. 保存参数信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

        // 6. 取出评论信息
        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0]
        }

      }) 
    
    // 3. 请求详情推荐数据
    getRecommentd()
      .then(res => {
        const data = res.data
        this.recomments = data.list
      })
  },
  mounted(){

  },
  updated() {

  },
  destroyed() {
    // 取消全局事件
    this.$bus.$off('imgLoad', this.itemImgLoadFn)
  },
  watch:{},
  computed:{},
  methods:{
    // 添加 vuex 的 mapActions 映射 
    ...mapActions(['addCart']),
    // 监听 img 加载完成事件
    imageLoad() {
      this.$refs.scroll.refresh()
      
      // 获取 正确的 offsetTop 值
      this.themeTopYs = []
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.params.$el.offsetTop)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recomnd.$el.offsetTop)
      this.themeTopYs.push(Number.MAX_VALUE)
    },
    // 监听 导航 点击事件
    navClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200)
    },
    contentScroll(postions) {
      // 获取 y 值
      const PositionsY = -postions.y

      // 和主题中的 值 进行对比
      // [0, 1001, 2000, 9100]
      // 0-1001     positionsY = 0
      // 1001-2000  positionsY = 1
      // 2000-9100  positionsY = 2
      // >9100      positionsY = 3

      let length = this.themeTopYs.length

      for(let i = 0; i < length - 1; i++) {
        if ( this.currentIndex !== i && (i < length - 1 && PositionsY >= this.themeTopYs[i] && PositionsY < this.themeTopYs[i + 1]) ) {
          this.currentIndex = i
          this.$refs.nav.currentIndex = this.currentIndex
        }
      }

      // 判断是否需要显示 BackTop 
      this.listenShowBackTop(postions)
    },
    // 监听 添加购物车 点击事件
    addToCart() {
      // 1. 获取 购物车 需要展示的信息 
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      product.iid = this.iid

      // 2. 将商品 添加到购物车中
      // this.$store.dispatch('addCart', product)
      //   .then(res => {
      //     console.log(res)
      //   })

      this.addCart(product)
        .then(res => {
          // 使用 自定义插件 
          this.$toast.show(res)
        })
    }
  },
}
</script>
<style scoped>
#detail {
  position: relative;
  z-index: 99;
  background-color: #fff;
  height: 100vh;
}

.detail-nav {
  position: relative;
  z-index: 99;
  background-color: #fff;
}

.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 58px;
  left: 0px;
  right: 0px;
}

</style>